package com.hatom.cameraxhelp.utils;


import com.blankj.utilcode.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IDCardUtil {
    public IDCardUtil() {
    }

    public static boolean is18ByteIdCardComplex(String idCard) {
        Pattern pattern1 = Pattern.compile("^(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)?$");
        Matcher matcher = pattern1.matcher(idCard);
        int[] prefix = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        int[] suffix = new int[]{1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2};
        if (!matcher.matches()) {
            return false;
        } else {
            Map<String, String> cityMap = initCityMap();
            if (cityMap.get(idCard.substring(0, 2)) == null) {
                return false;
            } else {
                int idCardWiSum = 0;

                int idCardMod;
                for (idCardMod = 0; idCardMod < 17; ++idCardMod) {
                    idCardWiSum += Integer.valueOf(idCard.substring(idCardMod, idCardMod + 1)) * prefix[idCardMod];
                }

                idCardMod = idCardWiSum % 11;
                String idCardLast = idCard.substring(17);
                if (idCardMod == 2) {
                    return idCardLast.equalsIgnoreCase("x");
                } else {
                    return idCardLast.equals(suffix[idCardMod] + "");
                }
            }
        }
    }

    /**
     * 根据身份证号获取性别
     *
     * @param IDCard
     * @return
     */
    public static String getSex(String IDCard) {
        String sex = "";
        if (!StringUtils.isEmpty(IDCard)) {
                if (Integer.parseInt(IDCard.substring(16).substring(0, 1)) % 2 == 0) {
                    sex = "女";
                } else {
                    sex = "男";
                }
        }
        return sex;
    }

    /**
     * 根据身份证号获取年龄
     *
     * @param IDCard
     * @return
     */
    public static Integer getAge(String IDCard) {
        Integer age = 0;
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (!StringUtils.isEmpty(IDCard)) {
                // 身份证上的年份
                String year = IDCard.substring(6).substring(0, 4);
                // 身份证上的月份
                String yue = IDCard.substring(10).substring(0, 2);
                // 当前年份
                String fyear = format.format(date).substring(0, 4);
                // 当前月份
                String fyue = format.format(date).substring(5, 7);
                // 当前月份大于用户出身的月份表示已过生日
            int i = Integer.parseInt(fyear) - Integer.parseInt(year);
            if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
                    age = i + 1;
                    // 当前用户还没过生日
                } else {
                    age = i;
                }
        }
        return age;
    }

    /**
     * 获取出生日期  yyyy年MM月dd日
     *
     * @param IDCard
     * @return
     */
    public static String getBirthday(String IDCard) {
        String birthday = "";
        String year = "";
        String month = "";
        String day = "";
        if (!StringUtils.isEmpty(IDCard)) {
            // 身份证上的年份
            year = IDCard.substring(6).substring(0, 4);
            // 身份证上的月份
            month = IDCard.substring(10).substring(0, 2);
            //身份证上的日期
            day = IDCard.substring(12).substring(0, 2);
            birthday = year + "年" + month + "月" + day + "日";
        }
        return birthday;
    }

    public static String getNativePlace(String IDCard){
        if (StringUtils.isEmpty(IDCard)||IDCard.length()<2){
            return "身份证号长度不合标准";
        }
        String citycode=IDCard.substring(0,2);
        return initCityMap().get(citycode);
    }

    private static Map<String, String> initCityMap() {
        Map<String, String> cityMap = new HashMap();
        cityMap.put("11", "北京");
        cityMap.put("12", "天津");
        cityMap.put("13", "河北");
        cityMap.put("14", "山西");
        cityMap.put("15", "内蒙古");
        cityMap.put("21", "辽宁");
        cityMap.put("22", "吉林");
        cityMap.put("23", "黑龙江");
        cityMap.put("31", "上海");
        cityMap.put("32", "江苏");
        cityMap.put("33", "浙江");
        cityMap.put("34", "安徽");
        cityMap.put("35", "福建");
        cityMap.put("36", "江西");
        cityMap.put("37", "山东");
        cityMap.put("41", "河南");
        cityMap.put("42", "湖北");
        cityMap.put("43", "湖南");
        cityMap.put("44", "广东");
        cityMap.put("45", "广西");
        cityMap.put("46", "海南");
        cityMap.put("50", "重庆");
        cityMap.put("51", "四川");
        cityMap.put("52", "贵州");
        cityMap.put("53", "云南");
        cityMap.put("54", "西藏");
        cityMap.put("61", "陕西");
        cityMap.put("62", "甘肃");
        cityMap.put("63", "青海");
        cityMap.put("64", "宁夏");
        cityMap.put("65", "新疆");
        cityMap.put("71", "台湾");
        cityMap.put("81", "香港");
        cityMap.put("82", "澳门");
        cityMap.put("91", "国外");
        return cityMap;
    }
}
